package com.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


import com.web.dao.ReceiveAddressDAO;
import com.web.model.ReceiveAddress;
import com.web.util.DBConnection;

public class ReceiveAddressDAOImp  implements ReceiveAddressDAO{
	
	private static final String FIND_BY_USERID = "select * from d_receive_address where user_id=?";

	@SuppressWarnings("finally")
	@Override
	public List<ReceiveAddress> findByUserId(int userId) throws Exception {
		
		Connection conn=null;
		PreparedStatement prep= null;
		ResultSet rs=null;
		List<ReceiveAddress> addressList = null;
		try{
			conn=DBConnection.getConnection();
			prep=conn.prepareStatement(FIND_BY_USERID);
			prep.setInt(1, userId);
			rs=prep.executeQuery();
			ReceiveAddress address = null;
			addressList = new ArrayList<ReceiveAddress>();
			while(rs.next()){
				address=new ReceiveAddress();
				address.setUserId(rs.getInt("user_id"));
				address.setReceiveName(rs.getString("receive_name"));
				address.setFullAddress(rs.getString("full_address"));
				address.setPostalCode(rs.getString("postal_code"));
				address.setPhone(rs.getString("mobile"));
				address.setPhone(rs.getString("phone"));
				
				addressList.add(address);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs,prep,conn);
			return addressList;
		}
	}

	
	
	@Override
	public ReceiveAddress findById(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void save(ReceiveAddress addr) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
